package com.youwe1.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youwe1.wiki.domain.Category;
import com.youwe1.wiki.domain.CategoryExample;
import com.youwe1.wiki.mapper.CategoryMapper;
import com.youwe1.wiki.req.CategoryQueryReq;
import com.youwe1.wiki.req.CategorySaveReq;
import com.youwe1.wiki.resp.CategoryQueryResp;
import com.youwe1.wiki.resp.PageResp;
import com.youwe1.wiki.util.CopyUtil;
import com.youwe1.wiki.util.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;

    public List<CategoryQueryResp> all() {
        CategoryExample categoryExample = new CategoryExample();
        // 排序
        categoryExample.setOrderByClause("sort asc");
        // 查询方法
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        // 将查询出来的数据处理，去掉无用数据，只返回CategoryResp里定义的属性
        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);
        return list;
    }

    public PageResp<CategoryQueryResp> list(CategoryQueryReq req) {
        CategoryExample categoryExample = new CategoryExample();
        // 排序
        categoryExample.setOrderByClause("sort asc");
        // where 条件
        // CategoryExample.Criteria criteria = categoryExample.createCriteria();

        List<Category> categoryList;
        // 第一页从1开始。而且只会对最近的一条查询方法有效!!!!!
        PageHelper.startPage(req.getPage(), req.getSize());
        // 查询方法
        categoryList = categoryMapper.selectByExample(categoryExample);
        if (req.getPage() > 1 && categoryList.isEmpty()) {
            // 如果当前页没有数据，页码就减一再次查询
            PageHelper.startPage(req.getPage() - 1, req.getSize());
            categoryList = categoryMapper.selectByExample(categoryExample);
        }
        // 将查询出来的数据处理，去掉无用数据，只返回CategoryResp里定义的属性
        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);

        // 生成pageInfo用于获取total
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);

        // 分页实例
        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setPages(pageInfo.getPages());
        pageResp.setPage(req.getPage());
        pageResp.setSize(req.getSize());
        pageResp.setList(list);
        return pageResp;
    }

    public void save(CategorySaveReq req) {
        Category category = CopyUtil.copy(req, Category.class);
        if (ObjectUtils.isEmpty(req.getId())) {
            // 新增
            category.setId(snowFlake.nextId());
            categoryMapper.insert(category);
        } else {
            // 更新
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }
}
